SlideShare a Scribd company logo
Class No.17  Data Structures http://ecomputernotes.com
Reference Variables The symbol “&” has a few different purposes depending on where it occurs in code. When it appears in front of a variable name, it is the address operator, i.e., it returns the address of the variable in memory. int x; int* ptr = &x; http://ecomputernotes.com
Reference Variables The symbol “&’ can also appear after a type in a function signature: For example, insert and remove from the BinarySearchTree class. void insert( const EType& x ); void remove( const EType& x ); http://ecomputernotes.com
Reference Variables Or, in case we designed the BinarySearchTree class to hold integers only, i.e., no templates  void insert( const int& x ); void remove( const int& x ); http://ecomputernotes.com
Reference Variables The “&” indicates a parameter that is a  reference variable . Consider the following three different functions: http://ecomputernotes.com
Reference Variables // example 1 int intMinus1( int oldVal) { oldVal = oldVal – 1; return oldVal; } http://ecomputernotes.com
Reference Variables // example 2 int intMinus2( int* oldVal) { *oldVal = *oldVal – 2; return *oldVal; } http://ecomputernotes.com
Reference Variables // example 3 int intMinus3( int& oldVal) { oldVal = oldVal – 3; return oldVal; } http://ecomputernotes.com
Reference Variables The caller function: calling  intMinus1 void caller() { int myInt = 31; int retVal; retVal = intMinus1( myInt ); cout << myInt << retVal; } http://ecomputernotes.com
Memory Organization Code Static data Stack Heap Process 1 (browser) Process 3 (word) Process 4 (ourtest.exe) Windows OS Process 2 (dev-c++) http://ecomputernotes.com
Reference Variables Call stack layout Parameters(caller) Local variables(caller) Return address(caller) Parameters(intMinus1) Local variables(intMinus1) Return address(intMinus1) sp Stack grows downwards http://ecomputernotes.com
Reference Variables Call stack layout when intMinus1 is called: 1052 1060 1068 1056 31 1072 ? caller’s other stuff   myInt retVal oldVal 31 calling function “caller” called function “intMinus1” stack grows downwards   sp
Reference Variables How are myInt and oldVal related? Passing myInt to the function intMinus1 results in a  copy  of myInt to be placed in parameter oldVal in the call stack. Alterations are done to the copy of “31” (stored in oldVal) and not the original myInt. http://ecomputernotes.com
Reference Variables The original myInt remains  unchanged . For this reason, this technique of passing parameters is called  pass by value . Alterations are done to the copy of “31” (stored in oldVal) and not the original myInt. http://ecomputernotes.com
Reference Variables Call stack layout after subtraction in intMinus1: 1052 1060 1068 1056 31 1072 ? caller’s other stuff   myInt retVal oldVal 31 calling function “caller” called function “intMinus1” stack grows downwards   sp 30
Reference Variables Call stack layout after return from intMinus1: 1068 31 1072 30 caller’s other stuff   myInt retVal calling function “caller” stack grows downwards   sp
Reference Variables We could have called  intMinus1  as void caller() { int retVal; retVal = intMinus1( 31 );// literal cout << myInt << retVal; } Because it is the value that is passed. We can always pass a literal or even an expression in call-by-value. http://ecomputernotes.com
Reference Variables If the programmer wanted to actually change a variable’s value from within a function, one way would be to send a pointer: void caller() { int retVal; int myInt = 31; retVal = intMinus2( &myInt ); cout << myInt << retVal; } Call this call-by-pointer. http://ecomputernotes.com
Reference Variables Call stack layout when intMinus2 is called: 1052 1060 1068 1056 31 1072 ? caller’s other stuff   myInt retVal oldVal 1072 calling function “caller” called function “intMinus2” stack grows downwards   sp
Reference Variables Call stack layout after  *oldVal = *oldVal – 2; 1052 1060 1068 1056 31 1072 ? caller’s other stuff   myInt retVal oldVal 1072 calling function “caller” called function “intMinus2” stack grows downwards   sp 29
Reference Variables Call stack layout after return from  intMinus2. 1068 31 1072 29 caller’s other stuff   myInt retVal calling function “caller” stack grows downwards   sp 29
Reference Variables Suppose we want a function to change an object. But we don’t want to send the function a copy. The object could be large and copying it costs time. We don’t want to use pointers because of the messy syntax. http://ecomputernotes.com
Reference Variables The answer:  call-by-reference  (or pass-by-reference): void caller() { int retVal; int myInt = 31; retVal = intMinus3( myInt ); cout << myInt << retVal; } http://ecomputernotes.com
Reference Variables The & after int means that oldVal is an integer reference variable. // example 3 int intMinus3( int& oldVal) { oldVal = oldVal – 3; return oldVal; } http://ecomputernotes.com
Reference Variables So what  is  a reference variable? The idea is: the integer object myInt is used exactly as it exists in the caller. The function simply reaches it through a  different name , oldVal. The function intMinus3 cannot use the name myInt because it is in the caller’s scope. But both variable names refer to the same object (same memory cell). http://ecomputernotes.com
Reference Variables Call stack layout when intMinus3 is called: 1052 1060 1068 1056 31 1072 ? caller’s other stuff   myInt retVal oldVal calling function “caller” called function “intMinus3” stack grows downwards   sp
Reference Variables Call stack layout when intMinus3 is called: 1052 1060 1068 1056 31 1072 ? caller’s other stuff   myInt retVal oldVal calling function “caller” called function “intMinus3” stack grows downwards   sp
Reference Variables Call stack layout after oldVal = oldVal - 3: 1052 1060 1068 1056 31 1072 ? caller’s other stuff   myInt retVal oldVal calling function “caller” called function “intMinus3” stack grows downwards   sp 28
Reference Variables Call stack layout after return from intMinus3: 1068 31 1072 28 caller’s other stuff   myInt retVal calling function “caller” stack grows downwards   sp 28
Reference Variables The compiler may actually implement call-by-reference using pointers as we did in example 2. The obtaining of address and de-referencing would be done behind the scene. We should think in terms of the “renaming” abstraction.  http://ecomputernotes.com

More Related Content

PDF
C++ Course - Lesson 2
PPTX
Chp4(ref dynamic)
PDF
Functions in python
PPT
PDF
Python Functions (PyAtl Beginners Night)
PDF
Python-02| Input, Output & Import
PPT
RECURSION IN C
PPTX
Pointers in C/C++ Programming
C++ Course - Lesson 2
Chp4(ref dynamic)
Functions in python
Python Functions (PyAtl Beginners Night)
Python-02| Input, Output & Import
RECURSION IN C
Pointers in C/C++ Programming

What's hot (19)

PPTX
Operating System Assignment Help
PDF
Storage classes arrays & functions in C Language
PDF
Python Modules, Packages and Libraries
PDF
Introduction to python
PPTX
PPTX
Pointer to function 2
PPT
Lecture 5
DOCX
Maharishi University of Management (MSc Computer Science test questions)
PDF
e computer notes - Reference variables
PPTX
Pointer basics
PPT
Prsentation on functions
PPTX
Pointer to function 1
PPTX
C++ lecture 03
PPT
Lecture 11 - Functions
PPTX
Function Pointer
PPTX
c++ pointers by Amir Hamza Khan (SZABISTIAN)
PPT
Pointers in C
PPTX
Learn c++ (functions) with nauman ur rehman
PDF
computer notes - Stack
Operating System Assignment Help
Storage classes arrays & functions in C Language
Python Modules, Packages and Libraries
Introduction to python
Pointer to function 2
Lecture 5
Maharishi University of Management (MSc Computer Science test questions)
e computer notes - Reference variables
Pointer basics
Prsentation on functions
Pointer to function 1
C++ lecture 03
Lecture 11 - Functions
Function Pointer
c++ pointers by Amir Hamza Khan (SZABISTIAN)
Pointers in C
Learn c++ (functions) with nauman ur rehman
computer notes - Stack
Ad

Viewers also liked (20)

PPT
computer notes - Data Structures - 26
PPT
computer notes - Data Structures - 31
PPT
computer notes - Data Structures - 7
PPT
computer notes - Data Structures - 5
PPT
computer notes - Data Structures - 25
PPT
computer notes - Data Structures - 2
PPT
computer notes - Data Structures - 18
PPT
computer notes - Data Structures - 1
PPT
computer notes - Data Structures - 37
PPT
computer notes - Data Structures - 36
PPT
computer notes - Data Structures - 32
PPT
computer notes - Data Structures - 8
PPT
computer notes - Data Structures - 11
PPT
computer notes - Data Structures - 21
PPT
computer notes - Data Structures - 6
PPT
computer notes - Data Structures - 27
PPT
computer notes - Data Structures - 12
PPT
computer notes - Data Structures - 39
PPT
computer notes - Data Structures - 33
PPT
computer notes - Data Structures - 19
computer notes - Data Structures - 26
computer notes - Data Structures - 31
computer notes - Data Structures - 7
computer notes - Data Structures - 5
computer notes - Data Structures - 25
computer notes - Data Structures - 2
computer notes - Data Structures - 18
computer notes - Data Structures - 1
computer notes - Data Structures - 37
computer notes - Data Structures - 36
computer notes - Data Structures - 32
computer notes - Data Structures - 8
computer notes - Data Structures - 11
computer notes - Data Structures - 21
computer notes - Data Structures - 6
computer notes - Data Structures - 27
computer notes - Data Structures - 12
computer notes - Data Structures - 39
computer notes - Data Structures - 33
computer notes - Data Structures - 19
Ad

Similar to computer notes - Data Structures - 17 (20)

PDF
computer notes - Reference variables
PPT
3.pptirgggggggggggggggggggggggggggrrrrrrrrrrger
PPT
Lecture#7 Call by value and reference in c++
PPTX
Function C++
PPTX
functions
PPT
8. C_Function is the most impornant in c
PDF
Chapter 1. Functions in C++.pdf
PDF
Chapter_1.__Functions_in_C++[1].pdf
PPTX
Unit-III.pptx
PPT
11 functions
PDF
Preprocessor directives
PPTX
Chapter 1 (2) array and structure r.pptx
PPTX
C++ Functions | Introduction to programming
PPTX
Chapter 4
PPTX
Programming in C sesion 2
PPTX
CHAPTER THREE FUNCTION.pptx
PPTX
Fundamental of programming Fundamental of programming
PPT
2.overview of c++ ________lecture2
PDF
MTPLs
computer notes - Reference variables
3.pptirgggggggggggggggggggggggggggrrrrrrrrrrger
Lecture#7 Call by value and reference in c++
Function C++
functions
8. C_Function is the most impornant in c
Chapter 1. Functions in C++.pdf
Chapter_1.__Functions_in_C++[1].pdf
Unit-III.pptx
11 functions
Preprocessor directives
Chapter 1 (2) array and structure r.pptx
C++ Functions | Introduction to programming
Chapter 4
Programming in C sesion 2
CHAPTER THREE FUNCTION.pptx
Fundamental of programming Fundamental of programming
2.overview of c++ ________lecture2
MTPLs

More from ecomputernotes (20)

PPT
computer notes - Data Structures - 30
PPT
computer notes - Data Structures - 20
PPT
computer notes - Data Structures - 15
DOC
Computer notes - Including Constraints
DOC
Computer notes - Date time Functions
DOC
Computer notes - Subqueries
DOC
Computer notes - Other Database Objects
PPT
computer notes - Data Structures - 28
PPT
computer notes - Data Structures - 4
PPT
computer notes - Data Structures - 13
DOC
Computer notes - Advanced Subqueries
DOC
Computer notes - Aggregating Data Using Group Functions
PPT
computer notes - Data Structures - 16
PPT
computer notes - Data Structures - 22
PPT
computer notes - Data Structures - 35
DOC
Computer notes - Enhancements to the GROUP BY Clause
DOC
Computer notes - Manipulating Data
DOC
Computer notes - Writing Basic SQL SELECT Statements
PPT
computer notes - Data Structures - 14
PPT
computer notes - Data Structures - 10
computer notes - Data Structures - 30
computer notes - Data Structures - 20
computer notes - Data Structures - 15
Computer notes - Including Constraints
Computer notes - Date time Functions
Computer notes - Subqueries
Computer notes - Other Database Objects
computer notes - Data Structures - 28
computer notes - Data Structures - 4
computer notes - Data Structures - 13
Computer notes - Advanced Subqueries
Computer notes - Aggregating Data Using Group Functions
computer notes - Data Structures - 16
computer notes - Data Structures - 22
computer notes - Data Structures - 35
Computer notes - Enhancements to the GROUP BY Clause
Computer notes - Manipulating Data
Computer notes - Writing Basic SQL SELECT Statements
computer notes - Data Structures - 14
computer notes - Data Structures - 10

Recently uploaded (20)

PPT
Teaching material agriculture food technology
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
1. Introduction to Computer Programming.pptx
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Getting Started with Data Integration: FME Form 101
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Machine learning based COVID-19 study performance prediction
PPTX
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
PDF
Approach and Philosophy of On baking technology
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PPTX
A Presentation on Artificial Intelligence
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
Programs and apps: productivity, graphics, security and other tools
Teaching material agriculture food technology
Reach Out and Touch Someone: Haptics and Empathic Computing
1. Introduction to Computer Programming.pptx
Diabetes mellitus diagnosis method based random forest with bat algorithm
Getting Started with Data Integration: FME Form 101
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Unlocking AI with Model Context Protocol (MCP)
Per capita expenditure prediction using model stacking based on satellite ima...
Building Integrated photovoltaic BIPV_UPV.pdf
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Machine learning based COVID-19 study performance prediction
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
Approach and Philosophy of On baking technology
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
gpt5_lecture_notes_comprehensive_20250812015547.pdf
A Presentation on Artificial Intelligence
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Encapsulation_ Review paper, used for researhc scholars
Programs and apps: productivity, graphics, security and other tools

computer notes - Data Structures - 17

  • 1. Class No.17 Data Structures http://ecomputernotes.com
  • 2. Reference Variables The symbol “&” has a few different purposes depending on where it occurs in code. When it appears in front of a variable name, it is the address operator, i.e., it returns the address of the variable in memory. int x; int* ptr = &x; http://ecomputernotes.com
  • 3. Reference Variables The symbol “&’ can also appear after a type in a function signature: For example, insert and remove from the BinarySearchTree class. void insert( const EType& x ); void remove( const EType& x ); http://ecomputernotes.com
  • 4. Reference Variables Or, in case we designed the BinarySearchTree class to hold integers only, i.e., no templates void insert( const int& x ); void remove( const int& x ); http://ecomputernotes.com
  • 5. Reference Variables The “&” indicates a parameter that is a reference variable . Consider the following three different functions: http://ecomputernotes.com
  • 6. Reference Variables // example 1 int intMinus1( int oldVal) { oldVal = oldVal – 1; return oldVal; } http://ecomputernotes.com
  • 7. Reference Variables // example 2 int intMinus2( int* oldVal) { *oldVal = *oldVal – 2; return *oldVal; } http://ecomputernotes.com
  • 8. Reference Variables // example 3 int intMinus3( int& oldVal) { oldVal = oldVal – 3; return oldVal; } http://ecomputernotes.com
  • 9. Reference Variables The caller function: calling intMinus1 void caller() { int myInt = 31; int retVal; retVal = intMinus1( myInt ); cout << myInt << retVal; } http://ecomputernotes.com
  • 10. Memory Organization Code Static data Stack Heap Process 1 (browser) Process 3 (word) Process 4 (ourtest.exe) Windows OS Process 2 (dev-c++) http://ecomputernotes.com
  • 11. Reference Variables Call stack layout Parameters(caller) Local variables(caller) Return address(caller) Parameters(intMinus1) Local variables(intMinus1) Return address(intMinus1) sp Stack grows downwards http://ecomputernotes.com
  • 12. Reference Variables Call stack layout when intMinus1 is called: 1052 1060 1068 1056 31 1072 ? caller’s other stuff myInt retVal oldVal 31 calling function “caller” called function “intMinus1” stack grows downwards sp
  • 13. Reference Variables How are myInt and oldVal related? Passing myInt to the function intMinus1 results in a copy of myInt to be placed in parameter oldVal in the call stack. Alterations are done to the copy of “31” (stored in oldVal) and not the original myInt. http://ecomputernotes.com
  • 14. Reference Variables The original myInt remains unchanged . For this reason, this technique of passing parameters is called pass by value . Alterations are done to the copy of “31” (stored in oldVal) and not the original myInt. http://ecomputernotes.com
  • 15. Reference Variables Call stack layout after subtraction in intMinus1: 1052 1060 1068 1056 31 1072 ? caller’s other stuff myInt retVal oldVal 31 calling function “caller” called function “intMinus1” stack grows downwards sp 30
  • 16. Reference Variables Call stack layout after return from intMinus1: 1068 31 1072 30 caller’s other stuff myInt retVal calling function “caller” stack grows downwards sp
  • 17. Reference Variables We could have called intMinus1 as void caller() { int retVal; retVal = intMinus1( 31 );// literal cout << myInt << retVal; } Because it is the value that is passed. We can always pass a literal or even an expression in call-by-value. http://ecomputernotes.com
  • 18. Reference Variables If the programmer wanted to actually change a variable’s value from within a function, one way would be to send a pointer: void caller() { int retVal; int myInt = 31; retVal = intMinus2( &myInt ); cout << myInt << retVal; } Call this call-by-pointer. http://ecomputernotes.com
  • 19. Reference Variables Call stack layout when intMinus2 is called: 1052 1060 1068 1056 31 1072 ? caller’s other stuff myInt retVal oldVal 1072 calling function “caller” called function “intMinus2” stack grows downwards sp
  • 20. Reference Variables Call stack layout after *oldVal = *oldVal – 2; 1052 1060 1068 1056 31 1072 ? caller’s other stuff myInt retVal oldVal 1072 calling function “caller” called function “intMinus2” stack grows downwards sp 29
  • 21. Reference Variables Call stack layout after return from intMinus2. 1068 31 1072 29 caller’s other stuff myInt retVal calling function “caller” stack grows downwards sp 29
  • 22. Reference Variables Suppose we want a function to change an object. But we don’t want to send the function a copy. The object could be large and copying it costs time. We don’t want to use pointers because of the messy syntax. http://ecomputernotes.com
  • 23. Reference Variables The answer: call-by-reference (or pass-by-reference): void caller() { int retVal; int myInt = 31; retVal = intMinus3( myInt ); cout << myInt << retVal; } http://ecomputernotes.com
  • 24. Reference Variables The & after int means that oldVal is an integer reference variable. // example 3 int intMinus3( int& oldVal) { oldVal = oldVal – 3; return oldVal; } http://ecomputernotes.com
  • 25. Reference Variables So what is a reference variable? The idea is: the integer object myInt is used exactly as it exists in the caller. The function simply reaches it through a different name , oldVal. The function intMinus3 cannot use the name myInt because it is in the caller’s scope. But both variable names refer to the same object (same memory cell). http://ecomputernotes.com
  • 26. Reference Variables Call stack layout when intMinus3 is called: 1052 1060 1068 1056 31 1072 ? caller’s other stuff myInt retVal oldVal calling function “caller” called function “intMinus3” stack grows downwards sp
  • 27. Reference Variables Call stack layout when intMinus3 is called: 1052 1060 1068 1056 31 1072 ? caller’s other stuff myInt retVal oldVal calling function “caller” called function “intMinus3” stack grows downwards sp
  • 28. Reference Variables Call stack layout after oldVal = oldVal - 3: 1052 1060 1068 1056 31 1072 ? caller’s other stuff myInt retVal oldVal calling function “caller” called function “intMinus3” stack grows downwards sp 28
  • 29. Reference Variables Call stack layout after return from intMinus3: 1068 31 1072 28 caller’s other stuff myInt retVal calling function “caller” stack grows downwards sp 28
  • 30. Reference Variables The compiler may actually implement call-by-reference using pointers as we did in example 2. The obtaining of address and de-referencing would be done behind the scene. We should think in terms of the “renaming” abstraction. http://ecomputernotes.com

Editor's Notes

  • #3: Start of lecture 17
  • #13: The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #16: The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #17: The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #20: The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #21: The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #22: End of lecture 2 The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #28: The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #29: The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #30: The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #31: End of Lecture 17